/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "usart_debug.h"
#include "RS485.h"
#include "tocs_app.h"
#include "card_app.h"
#include "FM175xx.h"
#include "io_app.h"
#include "wiegand.h"
#include "iap.h"
#include <string.h>

#define _10ms_		1
#define _20ms_		2
#define _50ms_		5
#define _100ms_		10
#define _2000ms_		200
extern uint8_t flag_10ms;
extern struct s_CardRead_Machine M_CardRead;
extern struct s_Buzzer buzzer_state;

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/


/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
uint16_t FM175xx_SysTick=0;
uint16_t Tocs_SysTick=0;
uint16_t IN_Port_SysTick=0;
uint16_t SW_DIP_SysTick=0;
uint16_t ReadCrad_SysTick=0;

uint32_t dely = 0xFFFFFF;

/* Private function prototypes -----------------------------------------------*/
void NVIC_USARTConfiguration(void);
void IWDG_Configure(void);


void NVIC_TIMConfiguration(void);
void TIM4_Config(void);
void TIM2_Config(void);
/* Private functions ---------------------------------------------------------*/


/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	SystemInit();
	/* Enable the LSI OSC */
  RCC_LSICmd(ENABLE);
  /* Wait till LSI is ready */
  while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
  {}
		
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA , ENABLE);
	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
	
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_Init(GPIOA,&GPIO_InitStructure);  	
	GPIO_ResetBits(GPIOA, GPIO_Pin_7);
		
	//IWDG_Configure();
	TTL_IO_Init();
	USART_COM1_Init();

	IWDG_ReloadCounter();
	
	IAP_Load_Device_CFG();
	
	FM17550_IO_Init();
	
	UI_Init();
	
	if(FM17550_PCD_TEST())
		Systerm_Start_Buzzer();
	
	SW_DIP_IO_Init();
	Update_SW_DIP();
	
	IWDG_ReloadCounter();
	
	//Tocs_HardWare_Init();

	Wiegand_Init();

	NVIC_TIMConfiguration();
	
	TIM4_Config();
	
	NVIC_USARTConfiguration();
	
	IO_App_Init();
	
	IWDG_ReloadCounter();
	Systerm_Start_Buzzer();
	
	IWDG_ReloadCounter();
	Systerm_Start_Led();
	
  M_CardRead.State = 1;
	M_CardRead.dely = 0;
	memset(&buzzer_state,0,7);
	while(1)
	{
		while(flag_10ms != 'Y')
		{
		}
		IWDG_ReloadCounter();
		flag_10ms = 0;

		Led_Task();
		Buzzer_Task();
		
		Tocs_Process();
		RS485_App_Check();
		
		IN_Port_SysTick++;
		FM175xx_SysTick++;
		SW_DIP_SysTick++;
		ReadCrad_SysTick++;
		
		FM17550_Read_Card();
		
		if(IN_Port_SysTick > _100ms_)
		{
			IN_Port_SysTick = 0;
			IN_Port_Task();			
		}
		
		if(SW_DIP_SysTick > _2000ms_)
		{
			SW_DIP_SysTick = 0;
			Update_SW_DIP();
			Update_DeviceID(sw_dip_id);
		}
	}
}
/*******************************************************************************
* Function Name  : TIM4_Config
* Description    : Systick init
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void TIM4_Config(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

    /* TIM4 clock enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

    TIM_TimeBaseStructure.TIM_Period = (1000 - 1);

    TIM_TimeBaseStructure.TIM_Prescaler = (320 - 1);

    TIM_TimeBaseStructure.TIM_ClockDivision = 0;

    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

    TIM_ClearITPendingBit(TIM4, TIM_IT_Update);

    TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);

    TIM_Cmd(TIM4, ENABLE); 
}
/*******************************************************************************
* Function Name  : NVIC_TIM4Configuration
* Description    : Systick init
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void NVIC_TIMConfiguration(void)
{ 
    NVIC_InitTypeDef NVIC_InitStructure;

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

    /* Enable the TIM5 gloabal Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	
    NVIC_Init(&NVIC_InitStructure);
	
	/* Enable the TIM5 gloabal Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	
    NVIC_Init(&NVIC_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	
    NVIC_Init(&NVIC_InitStructure);
}
/*******************************************************************************
* Function Name  : NVIC_TIM4Configuration
* Description    : Systick init
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void NVIC_USARTConfiguration(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

    /* Enable the TIM5 gloabal Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	
    NVIC_Init(&NVIC_InitStructure);	
}

void IWDG_Configure(void)
{
	IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);

	//IWDG counter clock: 32KHz(LSI) / 32 = 1KHz
	IWDG_SetPrescaler(IWDG_Prescaler_32);

	//Set counter reload value to 2500
	IWDG_SetReload(2500);

	//Reload IWDG counter
	IWDG_ReloadCounter();

	//Enable IWDG (the LSI oscillator will be enabled by hardware)
	IWDG_Enable();
}
/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
